<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <!--<script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>-->
    <!--<script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>-->
    <!--[endif]-->
    <script type="text/javascript" src="../../common/js/public.js"></script>
    <script type="text/javascript" src="../../common/js/publicUtils.js"></script>
    <style>
        .layui-input-inline,.buttondiv .layui-input-inline {
            float: left;
            width: 450px !important;
            margin-right: 10px;
        }
        .error{
            color: red;
            line-height: 20px;
        }
    </style>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form"  id="infdictsortform" lay-filter="infdictsortform">
            <input type="hidden" name="id">
            <div class="layui-form-item">
                <label class="layui-form-label" for="code"><span style="color: red;">*</span>编&emsp;&emsp;码</label>
                <div class="layui-input-inline">
                    <input type="text" name="code" id="code" lay-verify="title" autocomplete="off" placeholder="请输入编码" class="layui-input" >
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" for="name"><span style="color: red;">*</span>名&emsp;&emsp;称</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" id="name" autocomplete="off" class="layui-input pname"  placeholder="请输入名称">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label" for="pname">所属分类</label>
                    <div class="layui-input-inline">
                        <input type="text" id="pname" autocomplete="off" class="layui-input" >
                        <input type="hidden" id="pid" name="pid">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label" for="sort"><span style="color: red;">*</span>排&emsp;&emsp;序</label>
                    <div class="layui-input-inline">
                        <input type="text" name="sort" id="sort" autocomplete="off" class="layui-input" placeholder="请输入排序">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label" for="comment">备&emsp;&emsp;注</label>
                    <div class="layui-input-inline">
                        <textarea name="comment" id="comment" placeholder="请输入备注信息" class="layui-textarea" ></textarea>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    var AUflag;//1为保存2为更新
    $(function () {
        $(".site-doc-icon li").click(function () {
            $(".site-doc-icon li").removeClass("selectlist");
            $(this).addClass("selectlist");
        })

    })
    
    function voluate(flag,data) {
        AUflag = flag;
        if(flag == 1){//添加
            $("#pname").val(data.name);
            $("#pid").val(data.id);
        }else{//更新
            layui.use("form",function(){
                var form = layui.form;
                form.val("infdictsortform",{
                    "name":data.name,
                    "pid":data.pid,
                    "id":data.id,
                    "sort":data.sort,
                    "comment":data.comment,
                    "code":data.code
                })
                $("#pname").val(data.pname);
            })
        }

    }

    function submit(dicFlag){
        console.info($("#infdictsortform").serializeJson());
        var data = $("#infdictsortform").serializeJson();
        var url,remark;
        if(dicFlag==2){//保存或应用菜单项
            url = 'infdictitem/insertInfDictItem';
            remark = 2;
        }else{//保存和应用菜单分类
            url = 'infdictsort/insertInfdictsort';
            remark = 1;
        }
        var obj = $(validform(1,remark).form());
        if(!obj[0]){
            return false;
        }
        var result;
        publicAjax (null,url,data,false,null,function (r) {
            if(r.code==2000){
                laysuccess("添加成功",null);
                result = r;
            }else{
                layerror("添加失败",null);
                result = r;
            }
        })
        return result;

    }

    function updateInfdict(dicFlag){
        console.info($("#infdictsortform").serializeJson());
        var data = $("#infdictsortform").serializeJson();
        var url,remark;
        if(dicFlag==2){//保存或应用菜单项
            url = 'infdictitem/updateInfDictItem';
            remark = 2;
        }else{//保存和应用菜单分类
            url = 'infdictsort/updateInfdictsort';
            remark = 1;
        }
        var obj = $(validform(2,remark).form());
        if(!obj[0]){
            return false;
        }
        var result;
        publicAjax (null,url,data,false,null,function (r) {
            if(r.code==2000){
                laysuccess("修改成功",null);
                result = r;
            }else{
                layerror("修改失败",null);
                result = r;
            }
        })
        return result;
    }

    //编写表单验证函数validform，在验证按钮注册按钮点击事件内调用验证函数对象
    function validform(flag,dicFlag){//flag 1保存2更新;dicFlag操作数据类型，1字典分类,2为字典分类项
        /*关键在此增加了一个return，返回的是一个validate对象，这个对象有一个form方法，返回的是是否通过验证*/
        jQuery.validator.addMethod("codev",function(value,element,param){
            var data;
            var codevurl;
            if(dicFlag==2){
                codevurl = "infdictitem/selectInfDictItemByEntity"
                data = {"id":$("#id").val(),"code":value,"pid":$("#pid").val()};
            }else{
                codevurl = "infdictsort/selectInfdictsortByEntity"
                data = {"id":$("#id").val(),"code":value};
            }
            var obj = true;
            publicAjax (null,codevurl,data,false,null,function (r) {
                if(r.code==2000){
                    if(flag==1){//保存
                        if(r.data>0){
                            obj = false;
                        }else{
                            obj = true;
                        }
                    }else{//更新
                        if(r.data>1){
                            obj = false;
                        }else{
                            obj = true;
                        }
                    }

                }else{
                    obj = false;
                }
            })
            return obj;
        },"该编码已存在，请重新输入");
        return $("#infdictsortform").validate({
            rules : {
                code:{
                    required:true,
                    codev: true,
                    normalizer: function( value ) {
                        return $.trim( value );
                    }
                },
                name : {
                    required:true,
                    normalizer: function( value ) {
                        return $.trim( value );
                    }
                },
                pname: {
                    required:true,
                    normalizer: function( value ) {
                        return $.trim( value );
                    }
                },
                sort: {
                    required:true,
                    number:true,
                    normalizer: function( value ) {
                        return $.trim( value );
                    }

                },
                comment: {
                    maxlength: 100,
                    normalizer: function( value ) {
                        return $.trim( value );
                    }
                },
            },
            messages : {
                code : {
                    required: "字典编码不能为空"
                },
                name : {
                    required: "字典名称不能为空"
                },
                pname : {
                    required: "请选择字典目录"
                },
                sort : {
                    required:"请输入有效的数字",
                    number: "请输入有效的数字",
                },
                comment : {
                    maxlength: "备注长度不可超过100"
                }
            }
        });
    }


    </script>
</body>

</html>
